

package example.serialization;

import com.hazelcast.nio.serialization.FieldKind;
import com.hazelcast.nio.serialization.compact.CompactReader;
import com.hazelcast.nio.serialization.compact.CompactSerializer;
import com.hazelcast.nio.serialization.compact.CompactWriter;

import javax.annotation.Nonnull;

public class NamedDTOSerializer implements CompactSerializer<NamedDTO> {
    @Nonnull
    @Override
    public NamedDTO read(@Nonnull CompactReader reader) {
        String name = reader.getFieldKind("name") == FieldKind.STRING ? reader.readString("name") : "";

        int myint = reader.getFieldKind("myint") == FieldKind.INT32 ? reader.readInt32("myint") : 0;
        return new NamedDTO(name, myint);
    }

    @Override
    public void write(@Nonnull CompactWriter writer, @Nonnull NamedDTO object) {
        writer.writeString("name", object.name);
        writer.writeInt32("myint", object.myint);
    }

    @Nonnull
    @Override
    public String getTypeName() {
        return "namedDTO";
    }

    @Nonnull
    @Override
    public Class<NamedDTO> getCompactClass() {
        return NamedDTO.class;
    }
}
